package DDL;

import java.util.ArrayList;
import java.util.List;





import util.Methods;
//瀹炵幇DDL涓殑Create Table鍔熻兘
import DuiXiang.TableModel;

public class CreatTableParsing {
	private static boolean bChar=false;
	public static int creatTableParsing(String sqlSentence) {
		boolean bLeftBracket = false, bRightBracket = false;// 是否有“(”和“)”
		for (int i = 0; i < sqlSentence.length(); i++) {
			if (sqlSentence.charAt(i) == '(')
				bLeftBracket = true;
			else if (sqlSentence.charAt(i) == ')') {
				bRightBracket = true;
				break;
			}
		}
		if (bLeftBracket != true || bRightBracket != true
				|| sqlSentence.charAt(sqlSentence.length()-1) != ';')// SQL语句是否合法
			{ System.out.println("sql语句中没有“(”或“)”或“，”");return -1;//SQL语句不合法 
			
			}
		String[] sqlSentenceFrontArray = sqlSentence.split("\\(");
		String[] sqlFrontArray = sqlSentenceFrontArray[0].trim().split("\\s+");
		String[] sqlSentenceLastArray = sqlSentenceFrontArray[1].split("\\)");
		String[] sqlLastArray = sqlSentenceLastArray[1].split("\\s+");
		String[] sqlMiddleArray = sqlSentenceLastArray[0].split(",");
		String[] tableDetailArray;
		List<TableModel> tableDetail = new ArrayList<TableModel>();

		if (!(sqlFrontArray[0].equals("CREATE") || sqlFrontArray[0]
				.equals("create")))
		{ System.out.println("没有关键字create");return -1;//SQL语句不合法
		
		}
		if (!(sqlFrontArray[1].equals("TABLE") || sqlFrontArray[0]
				.equals("table")))
		{ System.out.println("没有关键字table");return -1;//SQL语句不合法
		
		}
		if (!(sqlLastArray[0].equals("INTO") || sqlLastArray[0].equals("into")))
		{ System.out.println("没有关键字into");return -1;//SQL语句不合法
		
		}
		String tableName = sqlFrontArray[2];
		String dataBaseName = sqlLastArray[1].substring(0,sqlLastArray[1].length()-1);
		for (int i = 0; i < sqlMiddleArray.length; i++) {
			TableModel tableModel = new TableModel();
			tableDetailArray = sqlMiddleArray[i].trim().split("\\s+");
			switch (tableDetailArray.length) {
			case 3:
				tableModel.setsFieldName(tableDetailArray[0]);
				
				if (tableDetailArray[2].equals("KEY"))
					tableModel.setbKey('y');
				else
					tableModel.setbKey('n');
				if(getSize(tableDetailArray[1])<0)
				{ System.out.println("类型错误");return -2;//没有这个类型
				
				}
				tableModel.setiSize(getSize(tableDetailArray[1]));
				if(bChar==false)
					tableModel.setsType(tableDetailArray[1]);
				else{
					tableModel.setsType(Methods.getType(tableDetailArray[1]));
					bChar=false;
				}
				tableModel.setbNullFlag('n');
				tableModel.setbValidFlag('y');
				break;
			case 4:
				tableModel.setsFieldName(tableDetailArray[0]);
				
				if(getSize(tableDetailArray[1])<0)
				{ System.out.println("类型错误");return -2;//没有这个类型
				
				}
				tableModel.setiSize(getSize(tableDetailArray[1]));
				if(bChar==false)
					tableModel.setsType(tableDetailArray[1]);
				else{
					tableModel.setsType(Methods.getType(tableDetailArray[1]));
					bChar=false;
				}
				if (tableDetailArray[2].equals("KEY"))
					tableModel.setbKey('y');
				else
					tableModel.setbKey('n');
				if (tableDetailArray[3].equals("NO_NULL"))
					tableModel.setbNullFlag('n');
				else
					tableModel.setbNullFlag('y');
				tableModel.setbValidFlag('y');
				break;
			case 5:
				tableModel.setsFieldName(tableDetailArray[0]);
				
				if(getSize(tableDetailArray[1])<0)
				{ System.out.println("类型错误");return -2;//没有这个类型
				
				}
				tableModel.setiSize(getSize(tableDetailArray[1]));
				if(bChar==false)
					tableModel.setsType(tableDetailArray[1]);
				else{
					tableModel.setsType(Methods.getType(tableDetailArray[1]));
					bChar=false;
				}
				if (tableDetailArray[2].equals("KEY"))
					tableModel.setbKey('y');
				else
					tableModel.setbKey('n');
				if (tableDetailArray[3].equals("NO_NULL"))
					tableModel.setbNullFlag('n');
				else
					tableModel.setbNullFlag('y');
				if (tableDetailArray[4].equals("VALID"))
					tableModel.setbValidFlag('y');
				else
					tableModel.setbKey('n');
				break;
			default:
			{ System.out.println("sql语句有错");return -1;//SQL语句不合法
			
			}
			}
			tableDetail.add(tableModel);
		}
		if(CreatTable.creatTable(dataBaseName,tableName,tableDetail))
			return 1;//建表成功
		else
		{ System.out.println("建表时错误");return -3;//建表时错误
		
		}

	}

	public static int getSize(String type) {//得到字长
		int low=0,high=0;
		for (int i = 0; i < type.length(); i++) {
			if (type.charAt(i) == '[')
				low = i;
			else if (type.charAt(i) == ']') {
				high = i;
				break;
			}
		}
		if(type.equals("int"))
			return 4;
		else if(type.equals("byte"))
			return 1;
		else if(type.equals("long"))
			return 8;
		else if(type.equals("char"))
			return 11;
		else if(type.equals("float"))
			return 4;
		else if(type.substring(0, low).equals("char")||type.substring(0, low).equals("varchar")){
			bChar=true;
			return Integer.parseInt(type.substring(low+1, high));
		}
		else return -1;
	}
}
